#include <rtthread.h>

#include <lwip/sockets.h>
#include "webclient.h"
#include "paho_mqtt.h"



#define MAX_CLIENT			8


#define BUF_LEN 				2048 
#define ACTION_CONTROL			1
#define ACTION_READ_STATUS		2	
#define ACTION_READ_STATUS_ACK	3
#define ACTION_REPORT_STATUS	4
#define ACTION_TRANS_RECV		5
#define ACTION_TRANS_SEND		6
#define	ACTION_PUSH_OTA			254	

#define HEAD_LEN                5       
#define UDP_RECV_ERROR          199
#define TCP_RECV_ERROR_BASE     100
#define LOG_IP_BUF_LENGTH		16

#define HARD_VERSION			"LINUX_LI"
#define SOFT_VERSION			"04030000"


#define G_SERVICE_DOMAIN 	"api.gizwits.com"
#define G_SERVICE_PORT		"80"
#define G_M2M_DOMAIN		"sandbox.gizwits.com"
#define G_M2M_PORT			"1883"
#define G_M2M_SSL_PORT		"8883"

#define DID_LENGTH			22


typedef struct _con_st	con_st;

struct _con_st
{
    char	mac[32];
    char	did[32];
    char	passcode[16];
    char	pk[48];
    char	pk_secret[48];
    char	hard_version[16];
    char	soft_version[16];
}; 


typedef struct _cloud_st cloud_st;

struct _cloud_st
{
	con_st		*con;
//	mqtt_broker_handle_t	broker;
	
	char		mqtt_server[128];
	int		mqtt_port;
	int		mqtt_fd;
	int		mqtt_ping_time;
#ifdef USE_SSL
	sslConnection_st	*ssl_mqtt;
#endif	
	char		recv_buf[BUF_LEN];
	int		recv_len;
	char		send_buf[BUF_LEN];
	int		send_len;

	char sub_topic[2][128];

	char	ota_info[BUF_LEN];
	void		*in;
	int		sn;
};

typedef struct _lan_st lan_st;

struct _lan_st
{
	con_st	*con;
	int	udp_server;
	int	tcp_server;
	int	client_fd[MAX_CLIENT];
	char	recv_buf[BUF_LEN];
	int	recv_len;
	char	send_buf[BUF_LEN];
	int	send_len;
	char	passcode[32];

	int	sn;
	void	*in;
};


enum CMD_FROM_TYPE
{
    CMD_FROM_LAN = 0,
    CMD_FROM_MQTT,
};

enum UDP_SEND_TYPE
{
    UDP_SEND_TYPE_DISCOVER = 0,
    UDP_SEND_TYPE_BOARDCAST,
};



int gagent_lan_init(lan_st *lan);

int gagent_cloud_register(void);

int gagent_cloud_provision(void);

int gagent_mqtt_init(cloud_st *cloud);

//#endif
